package com.xju.subjectsystem.controller;

import com.xju.subjectsystem.pojo.LoginInfo;
import com.xju.subjectsystem.pojo.Result;
import com.xju.subjectsystem.service.StudentService;
import com.xju.subjectsystem.service.TeacherService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;

/**
 * -*- coding:utf-8 -*-
 *
 * @projectName: Subjectsystem
 * @package: com.xju.subjectsystem.controller
 * @className: LoginController
 * @author: Eiard
 * @description: DONE : 学生和教师登录
 * @date: 2024-12-05 21:17
 */
@RestController
public class LoginController {


    private final TeacherService teacherService;
    private final StudentService studentService;

    public LoginController(TeacherService teacherService, StudentService studentService) {
        this.teacherService = teacherService;
        this.studentService = studentService;
    }


    /**
     * @methodName: teacherLogin
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 教师端登录
     * @author: Eiard
     * @date: 2024-12-16 16:37
     * @param: username     账号
     * @param: password     密码
     * @return: Result
     */
    @PostMapping("/teacherLogin")
    public Result teacherLogin(@RequestParam String username, @RequestParam String password) throws NoSuchAlgorithmException {

        LoginInfo info = teacherService.login(username, password);

        if (info == null) {
            return Result.error("用户名或者密码错误!");
        } else {
            return Result.success(info);
        }
    }

    /**
     * @methodName: studentLogin
     * @package: com.xju.subjectsystem.controller
     * @description: DONE : 学生端登录
     * @author: Eiard
     * @date: 2024-12-16 16:36
     * @param: username     账号
     * @param: password     密码
     * @return: Result
     */
    @PostMapping("/studentLogin")
    public Result studentLogin(@RequestParam String username, @RequestParam String password) throws NoSuchAlgorithmException {

        LoginInfo info = studentService.login(username, password);
        if (info == null) {
            return Result.error("用户名或者密码错误!");
        } else {
            return Result.success(info);
        }
    }

}
